package com.longchat.interceptor;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.longchat.dto.system.WeixinProperties;
import com.longchat.utils.Rc4Utils;
import com.longchat.utils.WeiXinUrl;
import com.longchat.utils.httpUtil.WebUtils;

public class WeixinLoginInterceptor implements HandlerInterceptor{
	
	private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(LoginInterceptor.class); 
	@Autowired
	private WeixinProperties properties;
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		
	
		Cookie[] cookies = request.getCookies();
		String openid="";
		if(cookies!=null){
			for(Cookie cookie:cookies){
				if(cookie.getName().equals("openid")){
					openid = Rc4Utils.decry_RC4(cookie.getValue(),Rc4Utils.KEY);
					break;
				}
			}
		}
		if(StringUtils.isEmpty(openid)){
			String url = request.getRequestURI();
			response.sendRedirect(request.getContextPath()+"/weixinAuth?resultUrl="+url);
			return false;
		}else{
			return true;
		}
	
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		// TODO Auto-generated method stub
		
	}

}
